package cn.hkctp.hibernate3.demo1;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import java.util.List;

/**
 * Hibernate入门案例的测试
 *
 */
public class HibernateTest1 {

    @Test
    // 查询所有记录:SQL
    public void demo7(){
        // 1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        // 2.构建Session工厂
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.通过工厂创建Session
        Session session = sessionFactory.openSession();
        // 4.开启事务
        Transaction tx = session.beginTransaction();

        // 5.操作
        // 查询所有:SQL
		/*SQLQuery query = session.createSQLQuery("select * from customer");
		List<Object[]> list = query.list();

		for (Object[] objs : list) {
			System.out.println(Arrays.toString(objs));
		}*/
        SQLQuery query = session.createSQLQuery("select * from customer");
        query.addEntity(Customer.class);
        List<Customer> list = query.list();
        for (Customer customer : list) {
            System.out.println(customer);
        }

        // 6.事务提交
        tx.commit();
        // 7.释放资源
        session.close();
        sessionFactory.close();

    }

    @Test
    // 查询所有:QBC
    public void demo6(){
        // 1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        // 2.构建Session工厂
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.通过工厂创建Session
        Session session = sessionFactory.openSession();
        // 4.开启事务
        Transaction tx = session.beginTransaction();

        // 5.操作:
        // 查询所有 :QBC.
		/*Criteria criteria = session.createCriteria(Customer.class);
		List<Customer> list = criteria.list();*/
        Criteria criteria = session.createCriteria(Customer.class);
        criteria.add(Restrictions.eq("name", "凤姐"));
        List<Customer> list = criteria.list();

        for (Customer customer : list) {
            System.out.println(customer);
        }

        // 6.事务提交
        tx.commit();
        // 7.释放资源
        session.close();
        sessionFactory.close();
    }

    //查询所有
    @Test
    public void demo5(){
        // 1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        // 2.构建Session工厂
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.通过工厂创建Session
        Session session = sessionFactory.openSession();
        // 4.开启事务
        Transaction tx = session.beginTransaction();

        //5. 操作
        Query query = session.createQuery("from Customer where name = ?");
        query.setParameter(0, "优酷");     //第一个参数：问号位置；第二个参数：查询value
        List<Customer> list = query.list();

        for (Customer customer : list) {
            System.out.println(customer);
        }


        // 6.事务提交
        tx.commit();
        // 7.释放资源
        session.close();
        sessionFactory.close();
    }

    //删除记录
    @Test
    public void demo4(){
        // 1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        // 2.构建Session工厂
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.通过工厂创建Session
        Session session = sessionFactory.openSession();
        // 4.开启事务
        Transaction tx = session.beginTransaction();

        // 5.操作
        // 删除记录有两种方式:
        // 5.1手动创建对象的方式
		/*Customer customer = new Customer();
		customer.setId(2);

		session.delete(customer);*/

        // 5.2先查询在删除的方式
        Customer customer = (Customer)session.get(Customer.class, 1);
        session.delete(customer);

        // 6.事务提交
        tx.commit();
        // 7.释放资源
        session.close();
        sessionFactory.close();
    }

    //修改记录
    @Test
    public void demo3(){
        //1.Hibernate框架加载核心配置文件（有数据库链接信息）
        Configuration configuration = new Configuration().configure();
        //2.创建一个SessionFactory.(获得Session--相当于链接对象)
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //3.获得Session对象。
        Session session = sessionFactory.openSession();
        //4.默认的情况下，事务是不自动提交
        Transaction tx = session.beginTransaction();

        //5.业务逻辑操作
        //修改记录
        Customer customer = (Customer) session.get(Customer.class,1);
//        Customer customer = new Customer();
//        customer.setId(2);
        customer.setAge(24);


        session.update(customer);

        //6.事务提交
        tx.commit();
        //7.释放资源
        session.close();
        sessionFactory.close();
    }

    //根据id进行查询
    @Test
    public void demo2(){
        //1.Hibernate框架加载核心配置文件（有数据库链接信息）
        Configuration configuration = new Configuration().configure();
        //2.创建一个SessionFactory.(获得Session--相当于链接对象)
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //3.获得Session对象。
        Session session = sessionFactory.openSession();
        //4.默认的情况下，事务是不自动提交
        Transaction tx = session.beginTransaction();

        //5.业务逻辑操作
        // 根据id进行查询 -- 使用get方式进行查询
        Customer customer = (Customer) session.get(Customer.class, 111);
        System.out.println(customer);

        // 根据id进行查询 -- 使用load方式进行查询
//        Customer customer = (Customer) session.load(Customer.class, 111);
//        System.out.println(customer.getId());


        //6.事务提交
        tx.commit();
        //7.释放资源
        session.close();
        sessionFactory.close();
    }

    //向数据库中插入一条记录
    @Test
    public void demo1(){
        //1.Hibernate框架加载核心配置文件（有数据库链接信息）
        Configuration configuration = new Configuration().configure();
        //2.创建一个SessionFactory.(获得Session--相当于链接对象)
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //3.获得Session对象。
        Session session = sessionFactory.openSession();
        //4.默认的情况下，事务是不自动提交
        Transaction tx = session.beginTransaction();
        //5.业务逻辑操作
        //向数据库中插入一条记录
        Customer customer = new Customer();
        customer.setName("优酷");
        customer.setAge(30);

        session.save(customer);

        //6.事务提交
        tx.commit();
        //7.释放资源
        session.close();
        sessionFactory.close();
    }
}
